package com.ruoyi.web.controller.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.entity.VO.order.OrderExportVO;
import com.ruoyi.common.entity.VO.order.ShopOrderSearchVO;
import com.ruoyi.common.entity.VO.order.ShopOrderVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.order.service.IBackOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 */
@Controller
@RequestMapping("/back/order")
public class BackOrderController extends BaseController {
    private String prefix = "order";

    @Value("${applet.appId}")
    private String appId;

    @Value("${applet.payKey}")
    private String payKey;

    @Value("${applet.mchId}")
    private String mchId;

    @Resource
    private IBackOrderService orderService;

    /**
     * 订单列表-页面
     *
     * @return
     */
    @RequiresPermissions("back:order:list")
    @GetMapping()
    public String list() {
        return prefix + "/list";
    }

    /**
     * 订单编辑-页面
     *
     * @return
     */
    @RequiresPermissions("back:order:edit")
    @GetMapping("edit/{orderSn}")
    public String edit(@PathVariable("orderSn") String orderSn, ModelMap mmap) {
        mmap.put("order", orderService.getDetailByOrderSn(orderSn));
        return prefix + "/edit";
    }

    /**
     * 订单详情-页面
     */
    @GetMapping("/detail/{sn}")
    public String detail(@PathVariable("sn") String sn, ModelMap mmap) {
        mmap.put("order", orderService.getDetailByOrderSn(sn));
        return prefix + "/detail";
    }


    /**
     * 订单列表（搜索）-数据处理
     *
     * @param shopOrderSearchVO
     * @return
     */
    @RequiresPermissions("back:order:list")
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(ShopOrderSearchVO shopOrderSearchVO) {
        if (ShiroUtils.getShopId() != 0) {
            shopOrderSearchVO.setShopId(ShiroUtils.getShopId());
        }
        startPage();
        List<ShopOrderVO> list = orderService.searchList(shopOrderSearchVO);
        return getDataTable(list);
    }

    /**
     * 编辑订单-数据处理
     *
     * @param shopOrderVO
     * @return
     */
    @Log(title = "总后台-订单管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:order:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopOrderVO shopOrderVO) {
        shopOrderVO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        shopOrderVO.setUpdateTime(new Date());
        return toAjax(orderService.update(shopOrderVO));
    }

    /**
     * 数据处理-订单导出
     *
     * @return
     */
    @Log(title = "总后台-订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopOrderSearchVO shopOrderSearchVO) {
        if (StringUtils.isNotEmpty(shopOrderSearchVO.getOrderSnStr())) {
            if (shopOrderSearchVO.getOrderSnStr().contains(",")) {
                String[] orderSnArr = Convert.toStrArray(shopOrderSearchVO.getOrderSnStr());
                if (orderSnArr.length > 100) {
                    error("导出数据不能超过100条！");
                }
                shopOrderSearchVO.setOrderSnArr(orderSnArr);
            }
        }
        String sheetName = "";
        Map<String, Object> map = shopOrderSearchVO.getParams();
        if (map.containsKey("beginTime") && map.containsKey("endTime")) {
            sheetName += map.get("beginTime") + "到" + map.get("endTime") + "订单";
        } else if (map.containsKey("month")) {
            sheetName += map.containsKey("month") + "月份订单";
        } else {
            sheetName += "自定义导出订单";
        }
        List<OrderExportVO> orderList = orderService.searchOrderExportList(shopOrderSearchVO);
        ExcelUtil<OrderExportVO> util = new ExcelUtil<>(OrderExportVO.class);
        return util.exportExcel(orderList, sheetName);
    }

}
